# @time     :2022/2/24
# @Author   :Simple
# @FileName :run.py
import os

import click
import pytest

from config import cases_path
from util.logger import log as logger
from util.browser import init_browser_info
from util.process import open_process_pool
from util.report import init_report


@click.command()
@click.option('-m', default="run", help='输入运行模式：run 或 debug.')
def run(m):
    """
    执行测试
    （开始前去config.py修改日志类型：调试为DEBUG、测试为INFO）
    """
    # start_clean_file()   # 还未完成。。。
    browser_infos = init_browser_info()
    device_infos_len = len(browser_infos)

    # if m is None or m == "run":
    open_process_pool(func=run_pro, parm=browser_infos, num=device_infos_len)
    # elif m == "debug":
    #     open_process_pool(func=run_debug, parm=device_infos, num=device_infos_len)


def run_pro(browser_info):
    """
    回归模式
    """
    rerun = '0'
    logger.info("回归模式，开始执行✈✈！")

    browser_name = browser_info['device_name']
    logger.info(f"run device is {browser_name}")
    report = init_report(browser_name)
    xml_report = os.path.join(report, "xml")
    html_report = os.path.join(report, "html")
    logger.info(f"测试用例路径： {cases_path}")

    pytest.main(["-s", "-v",
                 "--alluredir=" + xml_report,
                 cases_path,
                 "--reruns", rerun])
    allure_report = f"allure generate {xml_report} -c -o {html_report}"
    logger.info(allure_report)
    os.popen(allure_report)
    logger.info("运行结束，生成测试报告♥❤！")


if __name__ == "__main__":
    run()
